package org.micropayment.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

/**
 *
 * @author jj
 */
@Entity
@Table(name = "MicropaymentUser")
@NamedQueries({
    @NamedQuery(name = "MicropaymentUser.findAll", query = "SELECT m FROM MicropaymentUser m"),
    @NamedQuery(name = "MicropaymentUser.findById", query = "SELECT m FROM MicropaymentUser m WHERE m.id = :id"),
    @NamedQuery(name = "MicropaymentUser.findByAuthority", query = "SELECT m FROM MicropaymentUser m WHERE m.authority = :authority"),
    @NamedQuery(name = "MicropaymentUser.findByName", query = "SELECT m FROM MicropaymentUser m WHERE m.name = :name"),
    @NamedQuery(name = "MicropaymentUser.findByPhone", query = "SELECT m FROM MicropaymentUser m WHERE m.phone = :phone"),
    @NamedQuery(name = "MicropaymentUser.findByEmail", query = "SELECT m FROM MicropaymentUser m WHERE m.email = :email"),
    @NamedQuery(name = "MicropaymentUser.findByDateAdded", query = "SELECT m FROM MicropaymentUser m WHERE m.dateAdded = :dateAdded"),
    @NamedQuery(name = "MicropaymentUser.findByLastEntryDate", query = "SELECT m FROM MicropaymentUser m WHERE m.lastEntryDate = :lastEntryDate"),
    @NamedQuery(name = "MicropaymentUser.findByUserid", query = "SELECT m FROM MicropaymentUser m WHERE m.userid = :userid"),
    @NamedQuery(name = "MicropaymentUser.findByPassword", query = "SELECT m FROM MicropaymentUser m WHERE m.password = :password"),
    @NamedQuery(name = "MicropaymentUser.findByAccountExpired", query = "SELECT m FROM MicropaymentUser m WHERE m.accountExpired = :accountExpired"),
    @NamedQuery(name = "MicropaymentUser.findByAccountLocked", query = "SELECT m FROM MicropaymentUser m WHERE m.accountLocked = :accountLocked"),
    @NamedQuery(name = "MicropaymentUser.findByCredentialsExpired", query = "SELECT m FROM MicropaymentUser m WHERE m.credentialsExpired = :credentialsExpired")})
public class MicropaymentUser implements Serializable, UserDetails {
//public class MicropaymentUser implements Serializable, UserDetails {
        
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "authority", length = 50)
    @Enumerated(value = EnumType.STRING)
    private MicropaymentAuthority authority;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    @Column(name = "phone")
    private String phone;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @Column(name = "dateAdded")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateAdded;
    @Basic(optional = false)
    @Column(name = "lastEntryDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastEntryDate;
    @Basic(optional = false)
    @Column(name = "userid")
    private String userid;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @Column(name = "accountExpired")
    private Boolean accountExpired;
    @Basic(optional = false)
    @Column(name = "accountLocked")
    private Boolean accountLocked;
    @Basic(optional = false)
    @Column(name = "credentialsExpired")
    private Boolean credentialsExpired;
    @Lob
    @Column(name = "details")
    private String details;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "micropaymentUserId")
    private Collection<Account> accountCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "micropaymentUserId")
    private Collection<Token> tokenCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "microcreditUserId")
    private Collection<MicropaymentConsumer> micropaymentConsumerCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "micropaymentUserId")
    private Collection<MicropaymentUserAddress> micropaymentUserAddressCollection;
    @Basic(optional = false)
    @Column(name = "enabled", nullable = false)
    private Boolean enabled;

    public MicropaymentUser() {
    }

    public MicropaymentUser(Integer id) {
        this.id = id;
    }

    public MicropaymentUser(Integer id, MicropaymentAuthority authority, String name, String email, Date dateAdded, Date lastEntryDate, String userid, String password, Boolean accountExpired, Boolean accountLocked, Boolean credentialsExpired) {
        this.id = id;
        this.authority = authority;
        this.name = name;
        this.email = email;
        this.dateAdded = dateAdded;
        this.lastEntryDate = lastEntryDate;
        this.userid = userid;
        this.password = password;
        this.accountExpired = accountExpired;
        this.accountLocked = accountLocked;
        this.credentialsExpired = credentialsExpired;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public MicropaymentAuthority getAuthority() {
        return authority;
    }

    public void setAuthority(MicropaymentAuthority authority) {
        this.authority = authority;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(Date dateAdded) {
        this.dateAdded = dateAdded;
    }

    public Date getLastEntryDate() {
        return lastEntryDate;
    }

    public void setLastEntryDate(Date lastEntryDate) {
        this.lastEntryDate = lastEntryDate;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getAccountExpired() {
        return accountExpired;
    }

    public void setAccountExpired(Boolean accountExpired) {
        this.accountExpired = accountExpired;
    }

    public Boolean getAccountLocked() {
        return accountLocked;
    }

    public void setAccountLocked(Boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public Boolean getCredentialsExpired() {
        return credentialsExpired;
    }

    public void setCredentialsExpired(Boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public Collection<Account> getAccountCollection() {
        return accountCollection;
    }

    public void setAccountCollection(Collection<Account> accountCollection) {
        this.accountCollection = accountCollection;
    }

    public Collection<Token> getTokenCollection() {
        return tokenCollection;
    }

    public void setTokenCollection(Collection<Token> tokenCollection) {
        this.tokenCollection = tokenCollection;
    }

    public Collection<MicropaymentConsumer> getMicropaymentConsumerCollection() {
        return micropaymentConsumerCollection;
    }

    public void setMicropaymentConsumerCollection(Collection<MicropaymentConsumer> micropaymentConsumerCollection) {
        this.micropaymentConsumerCollection = micropaymentConsumerCollection;
    }

    public Collection<MicropaymentUserAddress> getMicropaymentUserAddressCollection() {
        return micropaymentUserAddressCollection;
    }

    public void setMicropaymentUserAddressCollection(Collection<MicropaymentUserAddress> micropaymentUserAddressCollection) {
        this.micropaymentUserAddressCollection = micropaymentUserAddressCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MicropaymentUser)) {
            return false;
        }
        MicropaymentUser other = (MicropaymentUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.micropayment.model.MicropaymentUser[id=" + id + "]";
    }

    public GrantedAuthority[] getAuthorities() {
        return new GrantedAuthority[] { new GrantedAuthorityImpl(
                        authority.toString()) };
    }

    public String getUsername() {
        return userid;
    }

    public boolean isAccountNonExpired() {
        //return !accountExpired;
        return getEnabled();
    }

    public boolean isAccountNonLocked() {
        //return !accountLocked;
        return getEnabled();
    }

    public boolean isCredentialsNonExpired() {
        //return !credentialsExpired;
        return getEnabled();
    }

    public boolean isEnabled() {
        return getEnabled();
    }

    /**
     * @return the enabled
     */
    public Boolean getEnabled() {
        return enabled;
    }

    /**
     * @param enabled the enabled to set
     */
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

}
